﻿using System;
using System.IO;
using System.Threading;

namespace com.fukefu
{
    public class Log
    {
        //单例模式
        private static Log uf = null;
        private static Object syncObject = new Object();
        public static Log GetInstance()
        {
            if (uf == null)
            {
                lock (syncObject)
                {
                    if (uf == null)
                    {
                        uf = new Log();
                    }
                }
            }
            return uf;
        }
        public MyDelegate3 SetControl;
        StreamWriter sw;
        private Log()
        {
            if (!Directory.Exists("log"))
            {
                Directory.CreateDirectory("log");
            }
            sw = new StreamWriter("log\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");
        }
        public void Write(string s)
        {
            s = string.Format("→{0,-3} {1,-8} {2}", Thread.CurrentThread.Name, DateTime.Now.ToLongTimeString(), s);
            ShowMessage(s);
            WriteLogFile(s);
        }
        //写入日志文件
        private void WriteLogFile(string messge)
        {
            sw.WriteLine(messge);
            sw.Flush();
        }
        //屏幕显示
        private void ShowMessage(string message)
        {
            if (SetControl != null)
            {
                SetControl("listBox1", ControlValue.ListBox, message);
            }
        }
    }
}
